/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DigiJoe;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.util.ArrayList;
import java.util.Set;
import javax.imageio.ImageIO;
import javax.swing.JComponent;

/**
 *
 * @author Arch
 */
public class MyCanvas extends JComponent {

    //need the view for data
    private GuiView view;

    //constructor
    public MyCanvas(GuiView view) {
        this.view = view;
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        //clear the area
        g.setColor(Color.white);
        g.fillRect(0, 0, getWidth(), getHeight());

        //if the network is loaded
        if (view.isLoaded()) {
            //draw the elements
            Set<Integer> k = view.getComponentImageList().keySet();
            for (Integer id : k) {
                try {
                    //get the image
                    ComponentImage a = view.getComponentImage(id);
                    
                    //draw the image
                    g.drawImage(a.getImage(), a.getPosition().getX(), a.getPosition().getY(), null);
                    
                    //oscilloscope needs special treatement
                    if (NetworkLoaderController.getDigitalElement(a.getId()).getType() == ElementType.Oscilloscope)
                    {
                        Oscilloscope osc = (Oscilloscope)NetworkLoaderController.getDigitalElement(a.getId());
                        //do the extra drawing
                        //4 places
                        //offsets: 5, 28, 51, 74
                        //kep betoltese
                        Image img = ImageIO.read(new File("images/oscon.jpg"));
                        
                        //get the position
                        Coordinate pos = a.getPosition();
                        
                        //get the memory
                        ArrayList<Sign> mem = osc.getMemory();
                        
                        //to hold the last 4 states
                        Sign[] signs = new Sign[] { Sign.Low,Sign.Low,Sign.Low,Sign.Low};
                        
                        //if size is smaller then 4
                        if (mem.size() < 4)
                        {
                            //get the last x signs
                            for (int i = 0; i < mem.size(); i++) {
                                signs[2-i] = mem.get(i);
                                
                            }
                        } 
                        else
                        {
                            //get the last 4 signs
                            for (int i = 0; i < signs.length; i++) {
                                signs[i] = mem.get(mem.size()-1-i);
                                
                            }
                        }
                        
                        //for each sign, if the sign is high display a red circle
                        for (int i = 0; i < signs.length; i++) {
                            if (signs[i] == Sign.High)
                            {
                                g.drawImage(img, pos.getX()+5+i*23, pos.getY()+34, null);
                            }
                            
                        }
                    }
                } catch (Exception ex) {
                }
            } 
            
            g.setColor(Color.black);
            //draw the wires
            //wires: [outelement, outport, inelement, inport]
            for (Integer[] wire : view.getWires()) {
                try {
                    //get the componentimages
                    ComponentImage fromComp = view.getComponentImage(wire[0]);
                    ComponentImage toComp = view.getComponentImage(wire[2]);
                    
                    //get the coordinates
                    Coordinate from = fromComp.getOutPort(wire[1]);
                    Coordinate to = toComp.getInPort(wire[3]);
                    
                    //draw the line (wire)
                    g.drawLine(from.getX(), from.getY(), to.getX(), to.getY());
                } catch (Exception ex) {
                }
            }
        }
    }

    //needed for layout, sets the size
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(960, 690);
    }

    //sets the minimum size
    @Override
    public Dimension getMinimumSize() {
        return getPreferredSize();
    }
}
